package com.example.springboot.controller;

import com.example.springboot.entity.CityEntity;
import com.example.springboot.entity.DeptEntity;
import com.example.springboot.service.impl.UserServiceImp;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName UserController
 * @Author wkk
 * @Date 2021/12/28 10:56
 */
@Controller
public class UserController {
    @Autowired
    private UserServiceImp userServiceImp;

    @RequestMapping(value = "/getAll")
    public String getAll(HttpServletRequest request) {
        PageInfo<CityEntity> pageInfo = userServiceImp.getAll();
        List<DeptEntity> deptList = userServiceImp.getDeptList();
        request.setAttribute("users", pageInfo);
        request.setAttribute("depts", deptList);
        return "index";
    }

    @GetMapping(value = "/del")
    @ResponseBody
    public String del(Integer id) {
        int delCount = userServiceImp.del(id);
        return delCount == 1 ? "ok" : "error";
    }

    @PostMapping(value = "/update")
    @ResponseBody
    public String update(@RequestBody CityEntity cityEntity) {
        int updateCount = userServiceImp.update(cityEntity);
        return updateCount == 1 ? "ok" : "error";
    }
}
